package cn.net.yugu.doraemon.springweb.config;

import cn.net.yugu.doraemon.springweb.advice.YgResponseBodyAdvice;
import cn.net.yugu.doraemon.springweb.exception.CommonExceptionHandler;
import cn.net.yugu.doraemon.springweb.filter.YgRequestParamsFilter;
import cn.net.yugu.doraemon.springweb.health.HealthCheckController;
import cn.net.yugu.doraemon.springweb.interceptor.YgWebSignCheckInterceptor;
import cn.net.yugu.doraemon.springweb.interceptor.YgHttpTraceInterceptor;
import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.core.annotation.Order;
import org.springframework.util.CollectionUtils;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * @description:
 * @author: wanghongli
 * @create: 2022-07-29 00:01:55
 */
@Slf4j
@Configuration
@EnableConfigurationProperties(value = {YgWebProperties.class})
@Import({YgResponseBodyAdvice.class, CommonExceptionHandler.class, HealthCheckController.class})
public class YgWebAutoConfiguration implements WebMvcConfigurer {
    private static final String DEFAULT_EXCLUDE_INTERCEPTOR_PATH = "/error";
    @Autowired
    private YgWebProperties ygWebProperties;

    @Override
    public void addCorsMappings(CorsRegistry registry) {
        if (ygWebProperties.isCorsEnable()) {
            registry.addMapping("/**")
                    .allowCredentials(true)
                    .allowedHeaders("*")
                    .allowedOriginPatterns("*")
                    .allowedMethods("GET", "POST", "OPTIONS");
        }
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        InterceptorRegistration logInterceptorRegistration = registry.addInterceptor(new YgHttpTraceInterceptor(ygWebProperties)).excludePathPatterns(DEFAULT_EXCLUDE_INTERCEPTOR_PATH);
        if (!CollectionUtils.isEmpty(ygWebProperties.getRequestLogExcludePathPattern())) {
            logInterceptorRegistration.excludePathPatterns(ygWebProperties.getRequestLogExcludePathPattern());
        }
        if (ygWebProperties.isSignEnable()) {
            InterceptorRegistration interceptorRegistration = registry.addInterceptor(new YgWebSignCheckInterceptor(ygWebProperties)).excludePathPatterns(DEFAULT_EXCLUDE_INTERCEPTOR_PATH);
            if (!CollectionUtils.isEmpty(ygWebProperties.getSignCheckExcludePathPattern())) {
                interceptorRegistration.excludePathPatterns(ygWebProperties.getSignCheckExcludePathPattern());
            }
            if (!CollectionUtils.isEmpty(ygWebProperties.getSignCheckIncludePathPattern())) {
                interceptorRegistration.addPathPatterns(ygWebProperties.getSignCheckIncludePathPattern());
            }
        }
    }

    @Bean
    @Order(0)
    public YgRequestParamsFilter paramsRequestFilter() {
        return new YgRequestParamsFilter();
    }
}
